package getData;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Properties;

public class GetDataFromFieldsOfFramesXML {

	//private List<String> listOfAllFramesInCurrentFrame;

	/**
	 * @param nameOfProjectWhereThisFramePlased 
	 * @param listOfProjectsFileName 
	 * @param args
	 */
	
	public String getXMLForFrames(String nameOfFrameToViewRecievedFromMainForm) {
		String resolutionOfFile = ".xml";
		String subDirectoryForFile = "src/framesXML/";
		String nameOfXMLForFrame = null;
		
		nameOfFrameToViewRecievedFromMainForm = nameOfFrameToViewRecievedFromMainForm.concat(resolutionOfFile);
		nameOfXMLForFrame = subDirectoryForFile.concat(nameOfFrameToViewRecievedFromMainForm);
		//System.out.println("nameOfFrameToViewRecievedFromMainForm: " + nameOfFrameToViewRecievedFromMainForm);
				
		return nameOfXMLForFrame;
	}

	public List<String> getAlreadyExistsListOfFramesInCurrentFrame(
			String nameOfXMLForFrame) {
		ArrayList<String> listOfAllFramesInAllProjects;
		listOfAllFramesInAllProjects =  new ArrayList<String>();
		listOfAllFramesInAllProjects.clear();
		
		try {
			Properties properties = createFileInputStream(nameOfXMLForFrame);
			@SuppressWarnings("rawtypes")
			Enumeration enuKeys = properties.keys();
			while (enuKeys.hasMoreElements()){
				
				String propertyKey = (String) enuKeys.nextElement();
							
				if (propertyKey.matches("Name of field") || propertyKey.matches("Appointment")
						|| propertyKey.matches("Editable") || propertyKey.matches("Unique")	
						|| propertyKey.matches("Could be null") || propertyKey.matches("How to fill")
						|| propertyKey.matches("Field type") || propertyKey.matches("Logic")
						|| propertyKey.matches("Comment") || propertyKey.matches("ORDEROFFIELDS")){} else {
							listOfAllFramesInAllProjects.add(propertyKey);
												}
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		return listOfAllFramesInAllProjects;
	}

	private Properties createFileInputStream(String nameOfXMLForFrame)
			throws FileNotFoundException, IOException,
			InvalidPropertiesFormatException {
		FileInputStream fileInput = new FileInputStream(nameOfXMLForFrame);
		Properties properties = new Properties();
		properties.loadFromXML(fileInput);
		fileInput.close();
		return properties;
	}
	
	private String removeSystemSymbols(String value) {
		value = value.replaceAll("\\, ", "|");
		value = value.replaceAll("\\[", "");
		value = value.replaceAll("\\]", "");
		return value;
	}

	
	public List<String> getSetOfKeysForFrames(String nameOfXMLForFrame) {
		List<String> listOfKeysForXMLWithNoOrder =  new ArrayList<String>();
		List<String> listOfKeysForXML =  new ArrayList<String>();
		List<String> orderOfKeysFromXML =  new ArrayList<String>();
		try {
			Properties properties = createFileInputStream(nameOfXMLForFrame);
			@SuppressWarnings("rawtypes")
			Enumeration enuKeys = properties.keys();
			while (enuKeys.hasMoreElements()){
				String propertyKey = (String) enuKeys.nextElement();
					if (!propertyKey.equals("ORDEROFFIELDS"))
						// let's count important properties
						{listOfKeysForXMLWithNoOrder.add(propertyKey);}
				}
			}
				catch (IOException e) {
				e.printStackTrace();
		}
		addOneCommonKeyToListInCorrectOrder("Name of field",
				listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		addOneCommonKeyToListInCorrectOrder("Appointment",
				listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		addOneCommonKeyToListInCorrectOrder("Editable",
				listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		addOneCommonKeyToListInCorrectOrder("Unique",
				listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		addOneCommonKeyToListInCorrectOrder("How to fill",
				listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		addOneCommonKeyToListInCorrectOrder("Field type",
				listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		//addOneCommonKeyToListInCorrectOrder("Logic",
		//		listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		addOneCommonKeyToListInCorrectOrder("Comment",
				listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		// work with rames
		//read order for frames:
		orderOfKeysFromXML = getListOfFieldsFromCurrentFrame (nameOfXMLForFrame, "ORDEROFFIELDS");
		for (String string : orderOfKeysFromXML) {
			
			System.out.println("orderOfKeysFromXML" + string);
		}
		//sort
		for (String string : orderOfKeysFromXML) {
			addOneCommonKeyToListInCorrectOrder(string,
					listOfKeysForXMLWithNoOrder, listOfKeysForXML);
		}
		//end
		for (String string : listOfKeysForXML) {
			System.out.println("input fields " + string);
		}
		return listOfKeysForXML;
	}

	private void addOneCommonKeyToListInCorrectOrder(String nameOfField,
			List<String> listOfKeysForXMLWithNoOrder,
			List<String> listOfKeysForXML) {
		for (String item : listOfKeysForXMLWithNoOrder) {
			if (item.equals(nameOfField)){
				listOfKeysForXML.add(item);
			}
		}
	}
	
	
	
	public List<String> getListOfFieldsFromCurrentFrame(String nameOfXMLForFrame, String nameOfKeyForProperty) {
		List<String> listOfAllFieldsInThisFrame =  new ArrayList<String>();
		try {
			Properties properties = createFileInputStream(nameOfXMLForFrame);
			listOfAllFieldsInThisFrame.clear();		
			@SuppressWarnings("rawtypes")
			Enumeration enuKeys = properties.keys();
			while (enuKeys.hasMoreElements()){
				
				String propertyKey = (String) enuKeys.nextElement();
			//	if (!propertyKey.equals("ORDEROFFIELDS")) {		
				if (propertyKey.matches(nameOfKeyForProperty)){							
					String value = properties.getProperty(propertyKey);
					value = removeSystemSymbols(value);
					String[] allFields = value.split("\\|");
							for (String oneField : allFields) {
							//	value = value.replaceAll("\\"", "");
								listOfAllFieldsInThisFrame.add(oneField);
							}
			//			}
					}
				}
			}
				catch (IOException e) {
				e.printStackTrace();
		}
		return listOfAllFieldsInThisFrame;
	}

	

	public List<String> getListOfAlreadySelectedFields(String nameOfXMLForFrameWithFieldsForAdding, String nameOfFrameWhereWeWillBeManagingFields) {
		List<String> listOfAllAlreadyFieldsInThisFrame =  new ArrayList<String>();
		try {
			Properties properties = createFileInputStream(nameOfXMLForFrameWithFieldsForAdding);
			@SuppressWarnings("rawtypes")
			Enumeration enuKeys = properties.keys();
			//System.out.println("file readed");
			while (enuKeys.hasMoreElements()){
				String propertyKey = (String) enuKeys.nextElement();
				if (!propertyKey.equals("ORDEROFFIELDS")) {
					//System.out.println("propertyKey " + propertyKey + " = " + nameOfFrameWhereWeWillBeManagingFields);
				if (propertyKey.equals(nameOfFrameWhereWeWillBeManagingFields)){
					//System.out.println("SUCCESS");
					String value = properties.getProperty(propertyKey);
					value = removeSystemSymbols(value);
					String[] allFields = value.split("\\|");
					listOfAllAlreadyFieldsInThisFrame.clear();
						for (String oneField : allFields) {
							listOfAllAlreadyFieldsInThisFrame.add(oneField);
							//System.out.println("oneField " + oneField);
							}
						}
					}
				}
			}
				catch (IOException e) {
				e.printStackTrace();
		}
		return listOfAllAlreadyFieldsInThisFrame;
	}
	
	public Object getListOfAllDataForCurrentKey(String nameOfXMLForFrame, String keyForField) {
		List<String> listOfAllFieldsInThisFrame =  new ArrayList<String>();
		try {
			Properties properties = createFileInputStream(nameOfXMLForFrame);
			@SuppressWarnings("rawtypes")
			Enumeration enuKeys = properties.keys();
			
			while (enuKeys.hasMoreElements()){
				String propertyKey = (String) enuKeys.nextElement();
				//if (!propertyKey.equals("ORDEROFFIELDS")) {
				if (propertyKey.equals(keyForField)){
				String value = properties.getProperty(propertyKey);
				value = removeSystemSymbols(value);
				System.out.println("input value" + value); 
				String[] allFields = value.split("\\|");
				listOfAllFieldsInThisFrame.clear();
						
						for (String oneField : allFields) {
							listOfAllFieldsInThisFrame.add(oneField);
						}
				}
				}
			//}
		}
				catch (IOException e) {
				e.printStackTrace();
				}
		return listOfAllFieldsInThisFrame;
	}

	public List<String> getListOfpropertiesForCurrentField(
			String nameOfXMLForFrame,
			String nameOfFieldWhichPropertiesWeWillBeChange) {
		List<String> listOfAllNamesOfFieldsInThisFrame =  new ArrayList<String>();
		listOfAllNamesOfFieldsInThisFrame.clear();
		try {
			Properties properties = createFileInputStream(nameOfXMLForFrame);
			@SuppressWarnings("rawtypes")
			Enumeration enuKeys = properties.keys();
			
			while (enuKeys.hasMoreElements()){
				String propertyKey = (String) enuKeys.nextElement();
				if (propertyKey.equals("Name of field")){
				String value = properties.getProperty(propertyKey);
				value = removeSystemSymbols(value);
				//System.out.println("input value" + value); 
				String[] allFields = value.split("\\|");
							for (String oneField : allFields) {
							listOfAllNamesOfFieldsInThisFrame.add(oneField);
					}
				}
			}
		}
				catch (IOException e) {
				e.printStackTrace();
				}
		return listOfAllNamesOfFieldsInThisFrame;
	}

	public String getValueForField(String nameOfXMLForFrame,
			int numberOfRowInLine, String jElementName) {
		List<String> listOfAllNamesOfFieldsInThisFrame =  new ArrayList<String>();
		listOfAllNamesOfFieldsInThisFrame.clear();
		try {
			Properties properties = createFileInputStream(nameOfXMLForFrame);
			@SuppressWarnings("rawtypes")
			Enumeration enuKeys = properties.keys();
			
			while (enuKeys.hasMoreElements()){
				String propertyKey = (String) enuKeys.nextElement();
				if (propertyKey.equals(jElementName)){
				String value = properties.getProperty(propertyKey);
				value = removeSystemSymbols(value);
				//System.out.println("input value" + value); 
				String[] allFields = value.split("\\|");
							for (String oneField : allFields) {
							listOfAllNamesOfFieldsInThisFrame.add(oneField);
						}
				}
			}
		}
				catch (IOException e) {
				e.printStackTrace();
				}
		String nameOfRequiredField = listOfAllNamesOfFieldsInThisFrame.get(numberOfRowInLine);
		return nameOfRequiredField;
	}

	public String getListOfAllFieldsForAddedFrames(
			String nameOfXMLForFrame, String currentFrameWhereWeSearchingFields) {
			String listOfAllFieldsForThisFrame = null;
			try {
				Properties properties = createFileInputStream(nameOfXMLForFrame);
				@SuppressWarnings("rawtypes")
				Enumeration enuKeys = properties.keys();
				while (enuKeys.hasMoreElements()){
					
					String propertyKey = (String) enuKeys.nextElement();
								
					if (propertyKey.matches("Name of field") || propertyKey.matches("Appointment")
							|| propertyKey.matches("Editable") || propertyKey.matches("Unique")	
							|| propertyKey.matches("Could be null") || propertyKey.matches("How to fill")
							|| propertyKey.matches("Field type") || propertyKey.matches("Logic")
							|| propertyKey.matches("Comment")  || propertyKey.matches("ORDEROFFIELDS")){}
							  else {
								System.out.println("propertyKey " + propertyKey);
								System.out.println("currentFrameWhereWeSearchingFields " + currentFrameWhereWeSearchingFields);
								 // if (currentFrameWhereWeSearchingFields.matches(propertyKey)){
								if (currentFrameWhereWeSearchingFields.equals(propertyKey)){
								//listOfAllFramesInAllProjects.add(propertyKey);
								System.out.println("SUCCESS");
								listOfAllFieldsForThisFrame = properties.getProperty(propertyKey);
								listOfAllFieldsForThisFrame = listOfAllFieldsForThisFrame.replaceAll("\\[", "");
								listOfAllFieldsForThisFrame = listOfAllFieldsForThisFrame.replaceAll("\\]", "");
								//value = removeSystemSymbols(value);
								//System.out.println("input value" + value); 
								//String[] allFields = value.split("\\|");
								//value.toString();
								//for (String oneField : allFields) {
								//	listOfAllFieldsForThisFrame.add(oneField);
								//}
								}
					}
				}
			}
				catch (IOException e) {
					e.printStackTrace();
				}
			return listOfAllFieldsForThisFrame;
			}
}